<?php
class ProductqrAction extends CommonAction {
	public
	function index() {
		if ( $this->_get( 'id' ) ) {
			$id = intval( $_GET[ 'id' ] );
			//当前站点数据ID
			$map[ 'site_id' ] = __SITEID__;
			$map[ 'id' ] = $id;
			//扩展字段
			$productModel = D( 'Product' );
			$productExtendsModel = D( 'Product_extends' );
			$extends = getProductExtends( $id );
			$this->assign( 'extends', $extends );
			//规格和属性
			$skuList = $productModel->getSKUListById( $id );
			$SKU = $productModel->getSKUById( $id );
			//商品数据
			$info = $productModel->where( array( 'id' => $id ) )->find();
			$memberInfo = $this->memberInfo;
			$this->assign( $info );
			
			$this->supplier = M('Supplier') -> where(array('id' => $info['supplier_id'])) -> find();
			
			$ModulesDB = D( 'Modules' );
			$this->display();
		}
	}


	//创建付款订单
	public
	function createorder() {
        if($_POST['supid']==$this->memberInfo['id']){
            $this->error('付款失败');
        }
		$orderDB = D( 'Order' );
		$membersaledb = M( 'Membermembersale' );
		$allProduct = M('Product') -> where(array('id' => $_POST['productid'])) -> select();
		$member_memberdb = M( 'Member_member' );
		$_POST[ 'siteid' ] = 0;
		//设置为非充值订单
		$_POST[ 'savemoney' ] = 0;
		$_POST[ 'site_id' ] = __SITEID__;
		$_POST[ 'relate_id' ] = __RELATEID__;
		$_POST[ 'status' ] = 1; 
		$_POST[ 'ordernumber' ] = build_order_no();
		$_POST[ 'create_time' ] = time();
		$_POST[ 'create_date' ] = strtotime( date( 'Y-m-d' ) );
		$_POST[ 'total' ] = $_POST['num'] * 0.01 * 100;
		$_POST[ 'member_id' ] = $this->memberInfo[ 'id' ] ? $this->memberInfo[ 'id' ] : 0;
		$_POST[ 'nickname' ] = $this->memberInfo[ 'nickname' ] ? $this->memberInfo[ 'nickname' ] : '';
		$_POST[ 'total' ] += $_POST[ 'tax_fee' ];
		$orderId = $orderDB->add( $_POST );
		if ( $orderId ) {
			//写入订单明细
			$saledb = M( 'Sale' );
			$saledata = array();
			$sni = 0;
			foreach ( $allProduct as $v ) {
				$saledata[ 'order_id' ] = $orderId;
				$saledata[ 'product_id' ] = $v[ 'id' ];
				$saledata[ 'member_id' ] = $_POST[ 'member_id' ];
				$saledata[ 'num' ] = intval($_POST['num']);
				$saledata[ 'site_id' ] = __SITEID__;
				$saledata[ 'price' ] = $v[ 'price' ];
				$saledata[ 'aff_money' ] = $v[ 'affmoney' ];
				$saledata[ 'total' ] = $v[ 'num' ] * $saledata[ 'price' ];
				$saledata[ 'attr' ] = empty( $v[ 'attrList' ] ) ? '' : $v[ 'attrList' ];
				$saledata[ 'supplier_id' ] = empty( $v[ 'supplier_id' ] ) ? '' : $v[ 'supplier_id' ];
				if ( $supplier_member_id > 0 ) {
					$saledata[ 'supplier_id' ] = $supplier_member_id;
				}
				$saledata[ 'categoryname' ] = $v[ 'categoryname' ];
				$saledata[ 'create_time' ] = $_POST[ 'create_time' ];
				$saledata[ 'title' ] = $v[ 'title' ];
				$saledata[ 'img_url' ] = $v[ 'img_url' ];
				$sale_id = $saledb->data( $saledata )->add();
				//写入销售记录与推荐人关系表
				//下单的人所有上家
				$memberpids = $member_memberdb->where( array( 'smember_id' => $saledata[ 'member_id' ] ) )->select();
				foreach ( $memberpids as $mk => $mv ) {
					$membersale[ 'order_id' ] = $orderId;
					$membersale[ 'site_id' ] = __SITEID__;
					$membersale[ 'sale_id' ] = $sale_id;
					$membersale[ 'product_id' ] = $v[ 'id' ];
					$membersale[ 'dateline' ] = $_POST[ 'create_time' ];
					$membersale[ 'member_id' ] = $mv[ 'member_id' ];
					$membersale[ 'deep' ] = $mv[ 'deep' ];
					$membersale[ 'price' ] = $saledata[ 'price' ];
					$membersale[ 'aff_money' ] = $saledata[ 'aff_money' ];
					$membersale[ 'num' ] = $saledata[ 'num' ];
					$membersale[ 'total' ] = $saledata[ 'total' ];

					$membersaledb->add( $membersale );
				}
				if ( $sni == 0 ) {
					$domain = C( 'DOMAIN_MAIN' );
					$picurl = 'http://' . $domain[ 0 ] . '/Public' . $v[ 'img_url' ];
				}
				$sni++;
			}
			C( 'URL_MODEL', 0 );
			redirect( U( 'Home/Payment/index', array( 'id' => $orderId ), true, false, true ) );
		} else {
			$this->error( $orderDB->getError() );
		}

	}
}